package com.scalar.chiptrack.operations.dataentry.actionCommands;

import com.scalar.chiptrack.ChipTrackExtendedAction;
import com.scalar.chiptrack.db.DBTransaction;
import com.scalar.chiptrack.commons.SessionKeys;
import com.scalar.chiptrack.operations.dataentry.forms.DieBankBinningInfoForm;
import com.scalar.chiptrack.operations.dataentry.dao.DieBankBinningInfoDAO;
import com.scalar.chiptrack.operations.dataentry.dao.SortDataEntryDAO;
import com.scalar.chiptrack.operations.dataentry.DataEntryHelper;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForm;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.ArrayList;

/**
 * User: Murali Krishna Yengala
 * Date: Jul 23, 2005
 * Time: 11:50:11 AM
 */
public class DieBankBinningInfoSaveCommand extends ChipTrackExtendedAction{
    public ActionForward executeRequest( ActionMapping mapping,
                                         ActionForm form,
                                         HttpServletRequest request,
                                         HttpServletResponse response) throws Exception
    {
        HashMap errors              = ( HashMap ) request.getAttribute( ERROR_MESSAGES );
        HashMap info                = ( HashMap ) request.getAttribute( INFO_MESSAGES );
        String forward = "dieBankBinningInfoEntry";
        DieBankBinningInfoForm dieBankBinningInfoForm = (DieBankBinningInfoForm) form;
        DBTransaction dbTransaction = null;
        boolean success = false;
        HttpSession session = request.getSession();

        if( !(isTokenValid( request )) )
        {
            errors.put("Lot Information is already Saved", null);
            DataEntryHelper.loadSortPartNumbers( request);
            DataEntryHelper.resetForm( dieBankBinningInfoForm);
            DataEntryHelper.loadSortPartNumbers( request);
            dieBankBinningInfoForm.setWafers(null);
            return mapping.findForward( forward );
        }
        try
        {
            int processStepId = Integer.parseInt( dieBankBinningInfoForm.getProcessStepId() );
            int lotId = Integer.parseInt( dieBankBinningInfoForm.getLotId());
            int goodBinsCount = dieBankBinningInfoForm.getGoodBinsCount();
            ArrayList wafersList = dieBankBinningInfoForm.getWafers();
            dbTransaction = DBTransaction.getInstance();
            dbTransaction.begin();
            DieBankBinningInfoDAO.saveSortLotWafers(dbTransaction, lotId, processStepId, goodBinsCount ,wafersList );
            SortDataEntryDAO.saveSortLotBinDataEntryInSortBinReleaseTable( dbTransaction, processStepId, lotId, goodBinsCount);
            SortDataEntryDAO.closeSortReleases( dbTransaction, lotId );
            success = true;
        }
        catch( Exception e)
        {
            errors.put(ERROR_INTERNAL_SERVER_ERROR, "Lot Information Could Not be Saved");
            DataEntryHelper.loadSortPartNumbers( request);
            DataEntryHelper.resetForm( dieBankBinningInfoForm);
        }
        finally
        {
            if ( success == true )
            {
                if ( dbTransaction != null )
                {
                    dbTransaction.commit ();
                }
                session.removeAttribute("dieBankBinningInfoForm");
                info.put ( "Lot Information Saved", null );
                DataEntryHelper.loadSortPartNumbers( request);
                dieBankBinningInfoForm.setWafers(null);
                resetToken( request );
            }
            else
            {
                errors.put ( "Lot Information Could Not be Saved", null );
                dbTransaction.rollback ();
                DataEntryHelper.loadSortPartNumbers( request);
                dieBankBinningInfoForm.setWafers(null);
            }
        }
        request.setAttribute (SessionKeys.INFO_MESSAGES, info);
        request.setAttribute (SessionKeys.ERROR_MESSAGES, errors);
        return mapping.findForward( forward );
    }
}
